// interface Product {
//   name: string;
//   age: number;
//   email?: string;
//   interests: string[];
// }

// function registerUser(name: string, age: number, email?: string, interests: string[] = []): Product {
//     if (age < 18) {
//         console.log("Sorry, you must be 18 or older to register.");
//     }
//     if (interests.some(interests =>!interests.includes("运动") &&!interests.includes("音乐") &&!interests.includes("阅读"))) {
//             console.log("Invalid interests selected.");}
//         return {
//         name,
//         age,
//         email,
//         interests,
//     };
// }

// registerUser("John", 20, "john@example.com", ["reading", "traveling"]);





// /**************************************2************************************************************/
// interface Product {
//     id: number;
//     name: string;
//     price: number;
// }
// interface CartItem {
//     product: Product;
//     quantity: number;
// }

// function addToCart(cart: CartItem[], product: Product, quantity: number): CartItem[] {
//     const existingItem = cart.find(item => item.product.id === product.id);
//     if (existingItem) {
//         console.log("Item already in cart.");
//         return cart;
//     } else {
//         cart.push({ product, quantity });
//     }
//     console.log(cart);
//     return cart;
// }

// function calculateTotal(cart: CartItem[]): number {
//     let total = 0;
//     for (const item of cart) {
//         total += item.product.price * item.quantity;
//     }
//     return total;
// }
// let cart: CartItem[] = [
//     { product: { id: 1, name: "phone", price: 2999 }, quantity: 2 },
//     { product: { id: 2, name: "earphone", price: 399 }, quantity: 1 },
// ];
// // addToCart(cart, { id: 1, name: "phone", price: 2999 }, 2);
// console.log(calculateTotal(cart));



// /*************************************3*************************************** */
// function handleResponse<T>(response: { data: T } | { error: string }): void{
//   if ("data" in response) {
//     console.log(response.data);
//   } else {
//     throw new Error(response.error);
//   }
// }

// const successRes = { data: { id: 1, name: "测试产品" } };
// handleResponse(successRes); // 应返回 { id: 1, name: "测试产品" }

// const errorRes = { error: "未找到资源" };
// handleResponse(errorRes); // 应抛出错误 "未找到资源"



// /*4*/
// interface Shape {
//   type: "circle" | "rectangle";
//   calculateArea(): number;
// }

// class Circle implements Shape {
//   type: "circle";
//   constructor(public radius: number) {
//     this.type = "circle";
//   }
//   calculateArea(): number {
//     return Math.PI * this.radius * this.radius;
//   }
// }

// class Rectangle implements Shape {
//   type: "rectangle";
//   constructor(public width: number, public height: number) {
//     this.type = "rectangle";
//   }
//   calculateArea(): number {
//     return this.width * this.height;
//   }
// }

// const circle = new Circle(5);
// console.log(circle.calculateArea()); // ≈78.54

// const rect = new Rectangle(4, 6);
// console.log(rect.calculateArea()); // 24



/**************************5***********************/
class TypedLocalStorage<T extends {[key: string]: any} = {[key: string]: any}> {
  private storage: {[key: string]: T} = {};

  setItem(key: string, value: T, expires?: Date): void {
    if (expires) {
      const now = new Date();
      const timestamp = now.getTime() + expires.getTime() - now.getTimezoneOffset() * 60000;
      localStorage.setItem(key, JSON.stringify({ value, timestamp }));
    } else {
      localStorage.setItem(key, JSON.stringify(value));
    }
  }

  getItem(key: string): T | null {
    const item = localStorage.getItem(key);
    if (item) {
      const parsedItem = JSON.parse(item);
      if (parsedItem.timestamp && new Date().getTime() > parsedItem.timestamp) {
        localStorage.removeItem(key);
        return null;
      }
      return parsedItem.value;
    }
    return null;
  }
}

const userStorage = new TypedLocalStorage<{name: string, age: number}>();

// 存储
userStorage.setItem("user", {name: "张三", age: 25});

// 读取
const user1 = userStorage.getItem("user"); // 类型为 {name: string, age: number} | null
console.log(user1);

